package com.ytzl.gotrip.rpc.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ytzl.gotrip.ext.utils.RedisUtils;
import com.ytzl.gotrip.model.GotripUser;
import com.ytzl.gotrip.rpc.api.RpcTokenService;
import com.ytzl.gotrip.utils.common.Constants;
import com.ytzl.gotrip.utils.common.DigestUtil;
import com.ytzl.gotrip.utils.common.ErrorCode;
import com.ytzl.gotrip.utils.common.UserAgentUtil;
import com.ytzl.gotrip.utils.exception.GotripException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * token 服务提供
 */
@Component
@Service(interfaceClass = RpcTokenService.class)
public class RpcTokenServiceImpl implements RpcTokenService {
    @Resource
    RedisUtils redisUtils;
    @Override
    public String generateToken(GotripUser gotripUser, String userAgent) {
        /** 创建token token:[MOBILE|PC]-userCode(md5)-userId-yyyyMMddHHmmss-浏览器标识
         pc端 2小时 移动端 不失效*/
        StringBuffer sbToken=new StringBuffer("token:");
        if (UserAgentUtil.checkAgent(userAgent)) {
            //移动设备
            sbToken.append("MOBILE-");
        }else{
            //pc
            sbToken.append("PC-");
        }
        String md5UserCode = DigestUtil.hmacSign(gotripUser.getUserCode());
        sbToken.append(md5UserCode).append("-");
        sbToken.append(gotripUser.getId()).append("-");
        String createTime = new SimpleDateFormat("yyyyMMddHHmmss").format( new Date());
        sbToken.append(createTime).append("-");
        String md5UserAgent = DigestUtil.hmacSign(userAgent, 6);
        sbToken.append(md5UserAgent);
        return sbToken.toString();
    }

    @Override
    public void saveToken(String token, GotripUser gotripUser) {
        if (token.contains("PC-")) {
            redisUtils.set(token, JSON.toJSONString(gotripUser), Constants.RedisExpire.SESSION_TIMEOUT);
        }else if (token.contains("MOBILE-")){
            redisUtils.set(token, JSON.toJSONString(gotripUser));
        }
    }

    @Override
    public boolean verifyToken(String token, String userAgent) {
        //token是否存在

        if (!redisUtils.exist(token)) {
            return false;
        }
        //创建token浏览器和当前浏览器token是否一致
        String md5UserAgent = DigestUtil.hmacSign(userAgent, 6);
       return token.contains(md5UserAgent);
    }

    @Override
    public void removeToken(String token) {
        //删除token时 给个缓冲期，避免其他请求使用token
        redisUtils.expire(token,3);
    }

    @Override
    public String replaceToke(String token, String userAgent) throws Exception {
        GotripUser gotripUser = this.getGotripUser(token,userAgent);
        long currentTime = redisUtils.ttl(token);
        if (currentTime>60*60){
            System.err.println("在保护期！！！！！");
            return token;
            //throw new GotripException("token在保护期",ErrorCode.AUTH_PARAMETER_ERROR);
        }
        String NewToken = generateToken(gotripUser,userAgent);
        if (currentTime==-1) {
            return token;
        }else if (currentTime>0){
            System.err.println("已经替换！！！！！");
            redisUtils.delete(token);
            saveToken(NewToken,gotripUser);
        }
        return NewToken;
    }

    @Override
    public GotripUser getGotripUser(String token, String userAgent) throws Exception{
        if (!this.verifyToken(token,userAgent)) {
            throw new GotripException("token无效", ErrorCode.AUTH_PARAMETER_ERROR);
        }
        //获取用户信息
        String gotripUserJson = (String) redisUtils.get(token);
        return JSON.parseObject(gotripUserJson, GotripUser.class);
    }
}
